package com.example.blog_system_ssm.controller;

import com.example.blog_system_ssm.mapper.UserMapper;
import com.example.blog_system_ssm.model.User;
import com.example.blog_system_ssm.tools.BCryptTest;
import com.example.blog_system_ssm.tools.Constant;
import com.example.blog_system_ssm.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @RequestMapping("/testE")
    public int testEX(){
        int n=10/0;
        return n;
    }


    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request){
        User userLogin=new User();
        userLogin.setUsername(username);
        userLogin.setPassword(password);

        User user=userMapper.login(userLogin);
        if (user!=null){
            System.out.println("登录成功");
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0,"登录成功",user);
        }else {
            System.out.println("登录失败");
            return new ResponseBodyMessage<>(-1,"登录失败",userLogin);
        }
    }



    @RequestMapping("/loginMD")
    public ResponseBodyMessage<User> loginMD(@RequestParam String username,@RequestParam String password, HttpServletRequest request){
       User user=userMapper.selectByName(username);
       if (user==null){
           return new ResponseBodyMessage<>(-1,"登录失败，用户名或密码错误",user);
       }else {
           boolean flg=bCryptPasswordEncoder.matches(password,user.getPassword());
           if (!flg){
               return new ResponseBodyMessage<>(-1,"登录失败，用户名或密码错误",user);
           }
           request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
           return new ResponseBodyMessage<>(0,"登录成功",user);
       }
    }



    @RequestMapping("/getlogininfo")
    public ResponseBodyMessage<User> getLoginInfo(HttpServletRequest request){
        User user=new User();
        user=(User) request.getSession().getAttribute(Constant.USERINFO_SESSION_KEY);

        if (user!=null){
            return new ResponseBodyMessage<>(0,"获取当前用户成功",user);
        }else {
            return new ResponseBodyMessage<>(-1,"获取当前用户失败",null);
        }
    }

    @RequestMapping("/getbloguserinfo")
    public ResponseBodyMessage<User> getBlogUserInfo(@RequestParam String blogId){
        int blogIId=Integer.parseInt(blogId);
        User user=userMapper.selectNameByblogId(blogIId);
        if (user==null){
            return new ResponseBodyMessage<>(-1,"此id无对应的作者信息",null);
        }else {
            return new ResponseBodyMessage<>(0,"成功查询此id对应的作者信息",user);
        }

    }

    @RequestMapping("/logout")
    public ResponseBodyMessage<Boolean> logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session=request.getSession(false);
        if (session==null){
            System.out.println("当前用户未登录，无法注销");
            return new ResponseBodyMessage<>(0,"当前用户未登录，无法注销",false);
        }
        session.removeAttribute(Constant.USERINFO_SESSION_KEY);
        return new ResponseBodyMessage<>(0,"注销成功",true);
    }


    @RequestMapping("/register")
    public ResponseBodyMessage<Boolean> registerUser(@RequestParam String username,@RequestParam String password){
        System.out.println("进入注册函数");
        User user=userMapper.selectByName(username);
        if (user!=null){
            return new ResponseBodyMessage<>(-1,"注册用户失败，当前已存在此用户名",false);
        }
       String passwordMD=bCryptPasswordEncoder.encode(password);
        System.out.println(passwordMD);
        int ret=userMapper.register(username,passwordMD);
        if (ret==1){
            return new ResponseBodyMessage<>(0,"注册用户成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"注册用户失败",false);
        }
    }
}
